﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Domain;
using Domain.NHibernate;
using OS.Infrastructure.Domain.UnitOfWork;
using ShortUrl.Models;

namespace ShortUrl.Controllers
{
    public class HomeController : Controller
    {
        private readonly IUnitOfWorkFactory unitOfWorkFactory;
        private readonly IAccountRepository accountRepository;
        private readonly IUrlRepository urlRepository;
        private readonly int DEFAULT_ACCOUNT = 1;

        public HomeController(IUnitOfWorkFactory unitOfWorkFactory, IAccountRepository accountRepository, IUrlRepository urlRepository)
        {
            this.unitOfWorkFactory = unitOfWorkFactory;
            this.accountRepository = accountRepository;
            this.urlRepository = urlRepository;
        }

    
        [HttpPost]
        public ActionResult Index(UrlModel newUrl)
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                var url = new Url();
                if (HttpContext.User.Identity.IsAuthenticated)
                    url.Account = accountRepository.GetAll().ToList().Find(x => x.Name == HttpContext.User.Identity.Name);
                else url.Account = accountRepository.Load(DEFAULT_ACCOUNT);
                url.LongUrl = newUrl.LongUrl;
                url.ShortKey = newUrl.ShortUrl;
                url.Date = newUrl.Date;
                url.Anonymous = Request.AnonymousID;
                accountRepository.Load(url.Account.Id).Urls.Add(url);
                urlRepository.Save(url);
                unitOfWork.Commit();
            }
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult ClickUrl(int id)
        {
            Url url;
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                url = urlRepository.Load(id);
                url.Click();
                unitOfWork.Commit();
            }
            return Redirect(url.LongUrl);
        }
        public ActionResult RedirectKey(string key)
        {
            Url url;
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                IList<Url> Urls = urlRepository.GetAll();
                url = Urls.ToList().Find(x => x.ShortKey == key);
                if (url != null)
                {
                    url.Click();
                    unitOfWork.Commit();
                    return Redirect(url.LongUrl);

                }
            }
                return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Delete(int id)
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                var deleteUrl = urlRepository.Load(id);
                deleteUrl.Account = null;
                urlRepository.Delete(deleteUrl);
                unitOfWork.Commit();
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Message = "Добро пожаловать на Short Url сервис!";
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
            {
                UrlModel urls = new UrlModel();
                if (HttpContext.User.Identity.IsAuthenticated)
                    urls.Urls =
                        urlRepository.GetUrlByAccount(accountRepository.GetByName(HttpContext.User.Identity.Name));
                else
                    urls.Urls =
                        urlRepository.GetUrlByAnonymous(Request.AnonymousID).ToList().FindAll(
                            x => x.Account.Id == DEFAULT_ACCOUNT);
                ViewData.Model = urls;
            }
            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
